package com.gaff.emp.core.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.PageR;
import com.gaff.emp.core.modules.sys.controller.dict.form.CreateForm;
import com.gaff.emp.core.modules.sys.controller.dict.form.UpdateForm;
import com.gaff.emp.core.modules.sys.dao.SysDictDao;
import com.gaff.emp.core.modules.sys.entity.SysDict;
import com.gaff.emp.core.modules.sys.service.SysDictService;
import com.gaff.emp.core.util.PageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@DependsOn("initDataBean")
public class SysDictServiceImpl implements SysDictService, InitializingBean {

    private static final List<SysDict> empty = Collections.emptyList();

    @Autowired
    private SysDictDao sysDictDao;

    private List<SysDict> cached = new ArrayList<>();

    @Override
    public PageR queryDictsByPage(Map<String, Object> params) {
        String key = (String) params.get("dictKey");
        IPage<SysDict> page = sysDictDao.selectPage(
                PageQuery.<SysDict>getPage(params),
                Wrappers.<SysDict>query()
                        .like(StrUtil.isNotBlank(key), "dict_key", key));
        return new PageR(page);
    }

    @Override
    public SysDict queryDictById(Integer id) {
        return sysDictDao.selectById(id);
    }

    @Override
    public void createDict(CreateForm form) {
        SysDict dict = new SysDict();
        BeanUtil.copyProperties(form, dict);
        sysDictDao.insert(dict);
    }

    @Override
    public void updateDict(UpdateForm form) {
        SysDict dict = sysDictDao.selectById(form.getId());
        if (dict == null) {
            throw new EmpException("字典不存在");
        }
        BeanUtil.copyProperties(form, dict);
        sysDictDao.updateById(dict);
    }

    @Override
    public void deleteById(Integer id) {
        SysDict dict = sysDictDao.selectById(id);
        if (dict == null) {
            return;
        }
        sysDictDao.deleteById(id);
    }

    @Override
    public SysDict queryOneDictByKey(String key) {
        List<SysDict> dicts = queryDictByKey(key);
        if (dicts == null || dicts.size() == 0) {
            return null;
        }
        return dicts.get(0);
    }

    @Override
    public List<SysDict> queryDictByKey(String key) {
        if (StrUtil.isBlank(key)) {
            return empty;
        }
        return cached.stream().filter(dict -> key.equals(dict.getDictKey())).collect(Collectors.toList());
    }


    @Scheduled(fixedDelay = 60 * 1000)
    public void reloadDicts() {
        List<SysDict> dicts = sysDictDao.selectList(Wrappers.emptyWrapper());
        this.cached = dicts;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reloadDicts();
    }
}
